
#import "AFHTTPRequest.h"


@implementation AFHTTPRequest


+ (void)requestWithRequesturl:(NSString * _Nullable)urlString
                   headerSign:(NSString * _Nullable)headerSign
                   httpMethod:(httpMethod)httpMethod
                    paramters:(_Nullable id)paramters
              blockCompletion: (nullable void(^)(id  _Nullable responseObject))finishBlock
                    withError:(nullable void(^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))errorBlock{
    if (urlString == nil) {
        urlString = @"";
    }
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    
    session.responseSerializer.acceptableContentTypes = [session.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    if (httpMethod == GET) {
        
        [session.requestSerializer setValue:headerSign forHTTPHeaderField:@"sign"];
        
        
        [session.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        session.requestSerializer.timeoutInterval = RequestTimeout;
        [session.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        
        
        
        [session GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                NSDictionary *backDict = responseObject;
                if ([[NSString stringWithFormat:@"%@",[backDict objectForKey:@"msg"]] isEqualToString:@"校验失败"]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"AgainGetCipher" object:nil];
                }
            }else{
                if (![responseObject isKindOfClass:[NSArray class]]) {
                    NSLog(@"返回信息：%@",responseObject);
                }
            }
            
            
            if (finishBlock) {
                finishBlock(responseObject);
            }
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            if (errorBlock) {
                errorBlock(task,error);
            }
        }];
        
    }
    
    if (httpMethod == POST) {
        
        AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
        [serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [serializer setValue:headerSign forHTTPHeaderField:@"sign"];
        
        [serializer willChangeValueForKey:@"timeoutInterval"];
        serializer.timeoutInterval = RequestTimeout;
        [serializer didChangeValueForKey:@"timeoutInterval"];
        
        session.requestSerializer = serializer;
        
        
        [session POST:urlString parameters:paramters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                NSDictionary *backDict = responseObject;
                if ([[NSString stringWithFormat:@"%@",[backDict objectForKey:@"msg"]] isEqualToString:@"校验失败"]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"AgainGetCipher" object:nil];
                }
            }else{
                if (![responseObject isKindOfClass:[NSArray class]]) {
                    NSLog(@"返回信息：%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
                }
            }
        
            
            
            if (finishBlock) {
                finishBlock(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (errorBlock) {
                errorBlock(task,error);
            }
        }];
        
    }
}

@end
